package com.hyx.api.utils;

import java.util.Random;

public class RandStrUtil {

    /**
     * 随机生成字母加数字的字符串
     *
     * @param length
     * @return
     */
    public static String getRandomString(int length) { //length表示生成字符串的长度
        char[] str = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
                'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
                'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++)
            sb.append(str[random.nextInt(str.length)]);
        return sb.toString();
    }

    /**
     * 随机生成纯数字的字符串
     *
     * @param length
     * @return
     */
    public static String createCode(int length) {
        char[] str = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++)
            sb.append(str[random.nextInt(str.length)]);
        return sb.toString();
    }

}
